package org.example.domain.activity.service;

import org.example.domain.activity.adapter.repository.IActivityRepository;
import org.example.domain.activity.model.entity.MarketProductEntity;
import org.example.domain.activity.model.entity.TrialBalanceEntity;
import org.example.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import org.example.domain.activity.model.valobj.TeamStatisticVO;
import org.example.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import org.example.types.design.framework.tree.StrategyHandle;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service

public class IIndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService {

    @Resource
    private DefaultActivityStrategyFactory defaultActivityStrategyFactory;

    @Resource
    private IActivityRepository repository;

    @Override
    public TrialBalanceEntity indexMarketTrial(MarketProductEntity marketProductEntity) throws Exception {
        StrategyHandle<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> strategyHandle = defaultActivityStrategyFactory.strategyHandle();
        TrialBalanceEntity trialBalanceEntity = strategyHandle.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
        return trialBalanceEntity;
    }

    @Override
    public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId, String userId, int ownerCount, int randomCount) {
        List<UserGroupBuyOrderDetailEntity> uniomAllList=new ArrayList<>();
        if(0!= ownerCount)
        {
            List<UserGroupBuyOrderDetailEntity> ownerList=repository.queryInProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
            if(null!=ownerList && !ownerList.isEmpty())
            {
                uniomAllList.addAll(ownerList);
            }

        }

        if(0!= randomCount)
        {
            List<UserGroupBuyOrderDetailEntity> randomList=repository.queryInProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, ownerCount);
            if(null!=randomList && !randomList.isEmpty())
            {
                uniomAllList.addAll(randomList);
            }

        }


            return uniomAllList;
    }

    @Override
    public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
        return repository.queryTeamStatisticByActivityId(activityId);
    }



}
